module page {

    export class PkFriendPage extends egret.Sprite {

        public static readonly Pk_Friends: number = 10002;
        public static readonly Hide_Pk_Friends: number = 10004;
        public static readonly Only_Submit_Score: number = 10005;


        private shareBitmap: egret.Bitmap;

        private rankBitmapData: egret.BitmapData;

        public constructor() {
            super();
        }

        public hide(): void {
            egret.log("PK好友 hide()");
            manager.DisplayManager.remove(this.shareBitmap);
            egret.stopTick(this.tickHandler, this);
            this.hidePK();
        }

        public static readonly PK_CANVAS_WIDTH: number = 200;
        public static readonly PK_CANVAS_HEIGHT: number = 150;

        public show(): void {
            console.log('PkFriendPage.SHOW')

            if (!manager.GameManager.isProduct) {
                return;
            }

            let canvas = manager.SystemManager.getSharedCanvas();
            let scale = manager.SystemManager.canvasWidth / 750;
            canvas.width = PkFriendPage.PK_CANVAS_WIDTH * scale;
            canvas.height = PkFriendPage.PK_CANVAS_HEIGHT * scale;

            if (this.rankBitmapData != null) {
                this.rankBitmapData.$dispose();
                this.rankBitmapData = null;
            }
            this.rankBitmapData = new egret.BitmapData(canvas);
            this.rankBitmapData.$deleteSource = false;
            const texture = new egret.Texture();
            texture._setBitmapData(this.rankBitmapData);
            this.shareBitmap = new egret.Bitmap(texture);
            this.shareBitmap.width = PkFriendPage.PK_CANVAS_WIDTH;
            this.shareBitmap.height = PkFriendPage.PK_CANVAS_HEIGHT;
            this.addChild(this.shareBitmap);
            egret.startTick(this.tickHandler, this);
        }

        public commit(score: number): void {
            if (!manager.GameManager.isProduct) {
                return;
            }
            let openDataContext = wx.getOpenDataContext();
            openDataContext.postMessage({
                command: PkFriendPage.Pk_Friends,
                score: score,
            });
        }

        private hidePK(): void {
            if (!manager.GameManager.isProduct) {
                return;
            }egret.log("PK好友 hidePK()");

            let openDataContext = wx.getOpenDataContext();
            openDataContext.postMessage({
                command: PkFriendPage.Hide_Pk_Friends
            });
        }

        public static DT: number = 100;

        private lastAt: number = 0;

        private tickHandler(dt: number): boolean {
            if (this.lastAt < 1) {
                this.lastAt = dt;
                return false;
            }
            if (dt - this.lastAt < PkFriendPage.DT) {
                return true;
            }
            this.lastAt = dt;
            egret.WebGLUtils.deleteWebGLTexture(this.rankBitmapData.webGLTexture);
            this.rankBitmapData.webGLTexture = null;
            return true;
        }

    }
}